package com.jingbit.ai.utils;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.hardware.camera2.CameraAccessException;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.seg.common.Term;
import com.jingbit.ai.AIApp;
import com.jingbit.ai.activity.FaceActivity;
import com.jingbit.ai.baidu.map.BDConstants;
import com.jingbit.ai.bean.AppInfo;
import com.jingbit.ai.bean.ContactInfo;
import com.jingbit.ai.bt.BluetoothService;
import com.jingbit.ai.bt.BluetoothUtil;
import com.jingbit.ai.helper.DateHelper;
import com.jingbit.ai.helper.Logger;
import com.jingbit.ai.helper.StringHelper;
import com.jingbit.ai.music.MusicService;
import com.jingbit.ai.weather.HFWeather;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CmdUtil {
    public static final String[] NEWS_APPS = {"人民日报", "央视新闻", "新华社", "央广网", "人民网+", "今日头条", "腾讯新闻", "网易新闻", "凤凰新闻", "澎湃新闻"};
    public static final String[] NOVEL_APPS = {"书旗小说", "番茄免费小说", "七猫免费小说", "宜搜小说", "起点读书", "纵横小说", "飞卢小说", "话本小说", "笔趣阁免费小说", "小说会"};
    public static final String[] ABOOK_APPS = {"喜马拉雅", "微信听书", "番茄畅听", "蜻蜓FM", "连尚免费读书", "懒人听书", "书旗小说", "七猫免费小说", "一米阅读", "得间免费小说"};
    public static final String[] VIDEO_APPS = {"优酷视频", "搜狐视频", "西瓜视频", "咪咕视频", "影视大全", "腾讯视频", "爱奇艺", "芒果TV", "七七影视大全"};
    public static SolarTermsUtil termsUtil = new SolarTermsUtil(System.currentTimeMillis());

    //    // 定义正则表达式
//    public static final String ACTION_REGEX = "^点点头|^摇摇头|^左转一下|^右转一下|^抬头|^低头|^^停止跳舞|^开始跳舞|^复位|" +
//            "^休眠|^关机|^打开.*|^关闭.*|^启动.*|重新^启动.*|^执行.*|播放.*|^暂停.*|播放.*歌曲|^停止.*|^调节.*|^控制.*|^(把|将).*调(高|低|亮|暗)|^(查下|查询).*|(今|明|后)天.*|" +
//            "(今|明|后)天.*(如何|怎么样)|^我想看.*|^我想听.*|^发送消息(到|给).*|^打电话.*|^拨号.*|^搜索.*|.*开了.*|^(声音|音量).*一点|^(上|下|前|后)一.*|" +
//            "^(把|将).*(调成|切换).*模式|^切换到.*模式|^连接.*|^(讲|说)一个.*|^设置.*的闹钟|.*后.*我.*|.*天.*叫.*|^帮我.*|^未来.*|(学|^模仿).*|^拍照|^笑一笑|^哭一哭";
    public static final String[] CMDS = {"^点点头", "^摇摇头", "^向左转", "^向右转", "^向后转", "^向前转", "^抬头", "^低头", "^停止跳舞", "^开始跳舞", "^复位", "^休眠", "^关机", "^打开.*", "^关闭.*",
            "^重新启动.*", "^启动.*", "^执行.*", "^播放.*", "^暂停.*", "^播放.*歌曲", "^停止.*", "^调节.*", "^控制.*", "^(把|将).*调(高|低|亮|暗)", "^(查下|查询).*", "(昨|今|明|后)天.*",
            "^我想看.*", "^我想听.*", "^发送消息(到|给).*", "^打电话.*", "^拨打", "^拨号.*", "^搜索.*", ".*开了.*", "^(声音|音量).*一点", "^(上|下|前|后)一.*", "^(把|将).*(调成|切换).*模式",
            "^切换到.*模式", "^连接.*", "^(讲|说)一个.*", "^设置.*的闹钟", ".*后.*我.*", ".*天.*叫.*", "^帮我.*", "^未来.*", "^模仿.*", "^拍照", "^笑一笑", "^哭一哭", ".*收款了", ".*开始叫卖了"};

    private static boolean wx_pay;

    public static String[] isCmd(String word) {
        for (String cmd : CMDS) {
            Pattern pattern = Pattern.compile(cmd);
            Matcher matcher = pattern.matcher(word);
            if (matcher.find()) {
                Log.d(AIApp.TAG, "handleMessage startChat isCmd：" + matcher.group());
                String operation = matcher.group();  // 不使用 group(1)
                if (!TextUtils.isEmpty(operation)) {
                    return new String[]{cmd, operation};
                }
            }
        }
        return null;
    }


    static boolean startDance = false;

    public static void executeCmd(FaceActivity ctx, MusicService musicService, BluetoothService btService, String cmd_rex, String cmd_text) throws CameraAccessException {
        List<String> targets = new ArrayList<>();
        TTSUtil.doSpeak("收到", false);
        AppInfo appInfo = null;
        switch (cmd_rex) {
            case "^点点头":
                upAndDownMove(btService, BluetoothService.MSG_PWM1_ANGLE);
                break;
            case "^摇摇头":
                leftAndRightMove(btService, BluetoothService.MSG_PWM3_ANGLE);
                break;
            case "^向左转":
                leftAndRightMove(btService, BluetoothService.MSG_PWM3_ANGLE, true);
                break;
            case "^向右转":
                leftAndRightMove(btService, BluetoothService.MSG_PWM3_ANGLE, false);
                break;
            case "^向后转":
                turnFrontAndBack(btService, BluetoothService.MSG_PWM3_ANGLE, false);
                break;
            case "^向前转":
                turnFrontAndBack(btService, BluetoothService.MSG_PWM3_ANGLE, true);
                break;
            case "^抬头":
                upAndDownMove(btService, BluetoothService.MSG_PWM1_ANGLE, false);
                break;
            case "^低头":
                upAndDownMove(btService, BluetoothService.MSG_PWM1_ANGLE, true);
                break;
            case "^停止跳舞":
                startDance = false;
                break;
            case "^开始跳舞":
                startDance = true;
                while (startDance) {
                    leftAndRightMove(btService, BluetoothService.MSG_PWM3_ANGLE);
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                    }
                    upAndDownMove(btService, BluetoothService.MSG_PWM1_ANGLE);
                    try {
                        Thread.sleep(300);
                    } catch (Exception e) {
                    }
                }
                break;
            case "^复位":
                btService.write(BluetoothService.MSG_RESET_SIGN, 0);
                break;
            case "^休眠":
                break;
            case "^关机":
                AppUtil.shutdownDevice(ctx);
                TTSUtil.doSpeak("开始^关机", false);
                break;
            case "^打开.*":
                targets = getTargets(cmd_text, new Nature[]{Nature.n});
                if (targets.isEmpty()) {
                    TTSUtil.doSpeak("你想^打开什么呀？", false);
                    break;
                }
                if (targets.get(0).contains("灯光") || targets.get(0).contains("手电筒")) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        AppUtil.controlLighter(ctx, true);
                        TTSUtil.doSpeak("已经^打开了", false);
                    }
                } else {
                    appInfo = AppUtil.getApp(ctx, targets.get(0));
                    startApp(ctx, appInfo);
                }
                break;
            case "^关闭.*":
                if (cmd_text.contains("电视")) {
                } else if (cmd_text.contains("冰箱")) {
                } else if (cmd_text.contains("热水器")) {
                } else if (cmd_text.contains("空调")) {
                } else {
                    targets = getTargets(cmd_text, new Nature[]{Nature.n});
                    if (targets.isEmpty()) {
                        TTSUtil.doSpeak("你想^关闭什么呀？", false);
                        break;
                    }
                    if (targets.get(0).contains("灯光") || targets.get(0).contains("手电筒")) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            AppUtil.controlLighter(ctx, false);
                            TTSUtil.doSpeak("已经^关闭了", false);
                        }
                    } else {
                        appInfo = AppUtil.getApp(ctx, targets.get(0));
                        if (appInfo != null) {
                            AppUtil.killProcess(ctx, appInfo.getPackageName());
                            TTSUtil.doSpeak("已经^关闭了", false);
                        } else {
                            TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                        }
                    }
                }
                break;
            case "^启动.*":
                if (cmd_text.contains("屏幕亮度自动")) {
                    AppUtil.setAutoScreenLightMode(ctx, true);
                } else if (cmd_text.contains("电视")) {
                } else if (cmd_text.contains("冰箱")) {
                } else if (cmd_text.contains("热水器")) {
                } else if (cmd_text.contains("空调")) {
                } else {
                    targets = getTargets(cmd_text, new Nature[]{Nature.n});
                    if (targets.isEmpty()) {
                        TTSUtil.doSpeak("你想^启动什么呀？", false);
                        break;
                    }
                    appInfo = AppUtil.getApp(ctx, targets.get(0));
                    startApp(ctx, appInfo);
                }
                break;
            case "^重新启动.*":
                if (cmd_text.contains("电视")) {
                } else if (cmd_text.contains("冰箱")) {
                } else if (cmd_text.contains("热水器")) {
                } else if (cmd_text.contains("空调")) {
                } else if (cmd_text.contains("手机")) {
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^执行.*":
                break;
            case "^播放.*":
                musicService.playByRnd();
                break;
            case "^播放.*歌曲":
                targets = getTargets(cmd_text, new Nature[]{Nature.nr, Nature.nr1, Nature.nr2, Nature.nrf, Nature.nrj});
                if (targets.isEmpty()) {
                    TTSUtil.doSpeak("没有找到你想听的歌！");
                    break;
                }
                searchMusic(musicService, targets.get(0));
                break;
            case "^暂停.*":
                if (cmd_text.contains("音乐")) {
                    musicService.pause();
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^停止.*":
                if (cmd_text.contains("音乐")) {
                    musicService.stop();
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^调节.*":
                if (cmd_text.contains("音量")) {
                } else if (cmd_text.contains("亮度")) {
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^控制.*":
                break;
            case "^(查下|查询).*":
            case "^搜索.*":
                String keyword = cmd_text.replace("查下", ""); // 要^搜索的关键字
                keyword = keyword.replace("查询", ""); // 要^搜索的关键字
                TTSUtil.doSpeak("开始^启动浏览器为你^搜索" + keyword, false);
                String url = "http://www.baidu.com/search?q=" + keyword;
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ctx.startActivity(intent);
                break;
            case "^(把|将).*调(大|小|高|低|亮|暗)":
                if (cmd_text.contains("音量")) {
                    AppUtil.setVolume(ctx, !(cmd_text.contains("高") || cmd_text.contains("大")));
                } else if (cmd_text.contains("屏幕")) {
                    AppUtil.setScreenLightValue(ctx, !cmd_text.contains("亮"));
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "(昨|今|明|后)天.*":
                if (cmd_text.contains("多少号") || cmd_text.contains("几号")) {
                    Date date = null;
                    String dayStr = "昨天";
                    if (cmd_text.startsWith("昨")) {
                        date = DateHelper.getYearstday();
                    } else if (cmd_text.startsWith("今")) {
                        dayStr = "今天";
                        date = new Date();
                    } else if (cmd_text.startsWith("明")) {
                        dayStr = "明天";
                        date = DateHelper.getTomorrow();
                    } else if (cmd_text.startsWith("后")) {
                        dayStr = "后天";
                        date = DateHelper.getAfterTomorrow();
                    }
                    termsUtil.init(date.getTime());
                    String dateStr = "";
                    if (cmd_text.contains("农历")) {
                        dateStr = termsUtil.getLunarDateString() + "，星期" + termsUtil.getDayOfWeekString() + "，" + termsUtil.getTermString();
                    } else {
                        dateStr = DateHelper.dateToString(date, DateHelper.ZH_DAY_FMT) + "，星期" + termsUtil.getDayOfWeekString();
                    }
                    TTSUtil.doSpeak(dayStr + "是" + dateStr);
                } else if (cmd_text.contains("哪一年")) {
                    TTSUtil.doSpeak(DateHelper.dateToString(new Date(), DateHelper.ZH_MIN_FMT));
                } else if (cmd_text.contains("下雨吗") || cmd_text.contains("出太阳吗")) {
                    handleWeather(ctx, cmd_text, 0);
                } else if (cmd_text.contains("可以去玩") || cmd_text.contains("可以出去玩")) {
                    handleWeather(ctx, cmd_text, 0);
                } else if (cmd_text.contains("可以去吃") || cmd_text.contains("可以出去吃")) {
                    handleWeather(ctx, cmd_text, 0);
                } else if (cmd_text.contains("天气")) {
                    handleWeather(ctx, cmd_text, 0);
                } else if (cmd_text.contains("气温") | cmd_text.contains("温度")) {
                    handleWeather(ctx, cmd_text, 1);
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^未来.*":
                if (cmd_text.contains("天气")) {
                    if (cmd_text.contains("3") || cmd_text.contains("三")) {
                        handleWeather(ctx, cmd_text, 3, 0, 0);
                    } else if (cmd_text.contains("5") || cmd_text.contains("一星期")) {
                        handleWeather(ctx, cmd_text, 7, 0, 0);
                    } else {
                        TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                    }
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "(当前|现在).*":
                if (cmd_text.contains("天气")) {
                    if (HFWeather.getInstance().getWeatherNow() != null) {
                        TTSUtil.doSpeak(HFWeather.getInstance().getWeatherNowStr());
                    } else {
                        HFWeather.getInstance().getWeather(ctx, 0, BDConstants.getLatLog(), Lang.ZH_HANT, Unit.METRIC);
                    }
                } else if (cmd_text.contains("时间") || cmd_text.contains("日期") || cmd_text.contains("哪一年")) {
                    TTSUtil.doSpeak(DateHelper.getFormatDate() + DateHelper.getWeekOfDate(DateHelper.getFormatDate()));
                } else if (cmd_text.contains("方位") | cmd_text.contains("位置")) {
                    if (BDConstants.location != null) {
                        TTSUtil.doSpeak("当前" + (cmd_text.contains("方位") ? "方位" : "位置") + BDConstants.getAddress());
                    } else {
                        TTSUtil.doSpeak("定位失败啦！");
                    }
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^我想看.*":
                if (cmd_text.contains("视频")) {
                    for (String novel : VIDEO_APPS) {
                        appInfo = AppUtil.getApp(ctx, novel);
                        if (appInfo != null) {
                            break;
                        }
                    }
                } else if (cmd_text.contains("电视")) {
                    for (String novel : VIDEO_APPS) {
                        appInfo = AppUtil.getApp(ctx, novel);
                        if (appInfo != null) {
                            break;
                        }
                    }
                } else if (cmd_text.contains("小说")) {
                    for (String novel : NOVEL_APPS) {
                        appInfo = AppUtil.getApp(ctx, novel);
                        if (appInfo != null) {
                            break;
                        }
                    }
                } else if (cmd_text.contains("新闻")) {
                    for (String novel : NEWS_APPS) {
                        appInfo = AppUtil.getApp(ctx, novel);
                        if (appInfo != null) {
                            break;
                        }
                    }
                }
                startApp(ctx, appInfo);
                break;
            case "^我想听.*":
                targets = getTargets(cmd_text, new Nature[]{Nature.nr, Nature.nr1, Nature.nr2, Nature.nrf, Nature.nrj, Nature.nt, Nature.ntcb, Nature.nto, Nature.ntu});
                if (cmd_text.contains("歌") | cmd_text.contains("歌曲")) {
                    targets = getTargets(cmd_text, new Nature[]{Nature.nr, Nature.nr1, Nature.nr2, Nature.nrf});
                    if (targets.isEmpty()) {
                        musicService.playByRnd();
                    } else {
                        searchMusic(musicService, targets.get(0));
                    }
                } else if (cmd_text.contains("小说") || cmd_text.contains("书")) {
                    if (!targets.isEmpty()) {
                        appInfo = AppUtil.getApp(ctx, targets.get(0));
                    } else {
                        for (String novel : ABOOK_APPS) {
                            appInfo = AppUtil.getApp(ctx, novel);
                            if (appInfo != null) {
                                break;
                            }
                        }
                    }
                    startApp(ctx, appInfo);
                } else if (cmd_text.contains("新闻")) {
                    if (!targets.isEmpty()) {
                        appInfo = AppUtil.getApp(ctx, targets.get(0));
                    } else {
                        for (String novel : NEWS_APPS) {
                            appInfo = AppUtil.getApp(ctx, novel);
                            if (appInfo != null) {
                                break;
                            }
                        }
                    }
                    startApp(ctx, appInfo);
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^发送消息(到|给).*":
                targets = getTargets(cmd_text, new Nature[]{Nature.nr, Nature.nr1, Nature.nr2, Nature.nrf, Nature.nrj, Nature.m});
                if (targets.isEmpty()) {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                    break;
                }
                ContactInfo contactInfo = AppUtil.getContact(ctx, targets.get(0));
                cmd_text = cmd_text.replace("发送消息到", "");
                cmd_text = cmd_text.replace("发送消息给", "");
                if (contactInfo != null && !contactInfo.getPhones().isEmpty()) {
                    cmd_text = cmd_text.replace(contactInfo.getName(), "");
                    AppUtil.sendSMS(ctx, contactInfo.getPhones().get(0), cmd_text);
                } else if (StringHelper.isMobileNumber(targets.get(0)) || StringHelper.isPhoneNumber(targets.get(0))) {
                    cmd_text = cmd_text.replace(targets.get(0), "");
                    AppUtil.sendSMS(ctx, targets.get(0), cmd_text);
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^打电话.*":
            case "^拨打.*":
            case "^拨号.*":
                targets = getTargets(cmd_text, new Nature[]{Nature.nr, Nature.nr1, Nature.nr2, Nature.nrf, Nature.nrj, Nature.m});
                if (targets.isEmpty()) {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                    break;
                }
                contactInfo = AppUtil.getContact(ctx, targets.get(0));
                if (contactInfo != null && !contactInfo.getPhones().isEmpty()) {
                    AppUtil.callPhone(ctx, contactInfo.getPhones().get(0));
                } else if (StringHelper.isMobileNumber(targets.get(0)) || StringHelper.isPhoneNumber(targets.get(0))) {
                    AppUtil.callPhone(ctx, targets.get(0));
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case ".*开了.*":
                if (cmd_text.startsWith("空调")) {

                } else if (cmd_text.startsWith("冰箱")) {

                } else if (cmd_text.startsWith("风扇")) {

                } else if (cmd_text.startsWith("蓝牙")) {
                    int state = AppUtil.getBluToothState();
                    switch (state) {
                        case BluetoothAdapter.STATE_OFF: // 蓝牙^关闭
                            TTSUtil.doSpeak("蓝牙已关闭");
                            break;
                        case BluetoothAdapter.STATE_TURNING_OFF: // 蓝牙正在^关闭
                            TTSUtil.doSpeak("蓝牙正在关闭");
                            break;
                        case BluetoothAdapter.STATE_ON: // 蓝牙打开
                            TTSUtil.doSpeak("蓝牙已打开");
                            break;
                        case BluetoothAdapter.STATE_TURNING_ON: // 蓝牙正在^打开
                            TTSUtil.doSpeak("蓝牙正在打开");
                            break;
                    }
                } else if (cmd_text.startsWith("WIFI") || cmd_text.startsWith("wifi")) {
                    TTSUtil.doSpeak(AppUtil.getWIFIState(ctx) ? "wifi连接了" : "wifi未连接");
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^(声音|音量).*一点":
                AppUtil.setVolume(ctx, !(cmd_text.contains("高") || cmd_text.contains("大")));
                break;
            case "^(上|下|前|后)一.*":
                if (musicService != null && musicService.isPlaying()) {
                    if (cmd_text.contains("上一") || cmd_text.contains("前一")) {
                        musicService.previous();
                    } else if (cmd_text.contains("下一") || cmd_text.contains("后一")) {
                        musicService.next();
                    } else {
                        TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                    }
                } else {

                }
                break;
            case "^(把|将).*(调成|切换).*模式":
                if (cmd_text.startsWith("空调")) {

                } else if (cmd_text.startsWith("冰箱")) {

                } else if (cmd_text.startsWith("风扇")) {

                } else if (cmd_text.startsWith("音乐")) {
                    handlePlayMode(musicService, cmd_text);
                } else if (cmd_text.startsWith("手机")) {
                    handlePhone(ctx, cmd_text);
                } else if (cmd_text.startsWith("灯光")) {
                } else if (cmd_text.startsWith("摄像头")) {
                    if (cmd_text.contains("前置")) {

                    } else if (cmd_text.contains("后置")) {

                    } else {

                    }
                } else {

                }
                break;
            case "^切换到.*模式":
                handlePhone(ctx, cmd_text);
                break;
            case "^连接.*":
                if (cmd_text.startsWith("空调")) {

                } else if (cmd_text.startsWith("冰箱")) {

                } else if (cmd_text.startsWith("风扇")) {

                } else if (cmd_text.startsWith("蓝牙")) {
                    int state = AppUtil.getBluToothState();
                    switch (state) {
                        case BluetoothAdapter.STATE_OFF: // 蓝牙^关闭
                            TTSUtil.doSpeak("蓝牙已^关闭");
                            break;
                        case BluetoothAdapter.STATE_TURNING_OFF: // 蓝牙正在^关闭
                            TTSUtil.doSpeak("蓝牙正在^关闭");
                            break;
                        case BluetoothAdapter.STATE_ON: // 蓝牙^打开
                            TTSUtil.doSpeak("蓝牙已^打开");
                            break;
                        case BluetoothAdapter.STATE_TURNING_ON: // 蓝牙正在^打开
                            TTSUtil.doSpeak("蓝牙正在^打开");
                            break;
                    }
                } else if (cmd_text.startsWith("WIFI") || cmd_text.startsWith("wifi")) {
                    TTSUtil.doSpeak(AppUtil.getWIFIState(ctx) ? "wifi连接了" : "wifi未连接");
                }
                break;
            case "^(讲|说)一个.*":
                break;
            case "^设置.*的闹钟":
                int[] dateTime = AlarmUtil.parseTime(cmd_text);
                if (dateTime != null) {
                    AlarmUtil.startAlarm(ctx, dateTime[0], dateTime[1], 0);
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case ".*后.*我.*":
                dateTime = AlarmUtil.parseTime(cmd_text);
                if (dateTime != null) {
                    AlarmUtil.startAlarm(ctx, dateTime[0], dateTime[1], 0);
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case ".*天.*叫.*":
                dateTime = AlarmUtil.parseTime(cmd_text);
                if (dateTime != null) {
                    AlarmUtil.startAlarm(ctx, dateTime[0], dateTime[1], 0);
                } else {
                    TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
                }
                break;
            case "^帮我.*":
                break;
            case "^模仿.*":
                targets = getTargets(cmd_text, new Nature[]{Nature.nba});
                Logger.log(AIApp.TAG, "^模仿.*", targets);
                if (targets.isEmpty()) {
                    TTSUtil.doSpeak("无法模仿哦");
                    break;
                }
                String music = musicService.searchMusic(targets.get(0), true).toString();
                if (TextUtils.isEmpty(music)) {
                    TTSUtil.doSpeak("无法模仿哦");
                    break;
                }
                MediaUtil.playOnlineAudio(music);
                break;
            case "^拍照":
//                ctx.takePhoto();
                break;
            case "^笑一笑":
                searchMusic(musicService, "笑");
                break;
            case "^哭一哭":
                searchMusic(musicService, "哭");
                break;
            case "^等一等":
                ctx.showQrcode(wx_pay);
                break;
            case ".*收款了":
                wx_pay = cmd_text.contains("微信");
                ctx.showQrcode(wx_pay);
                break;
            case ".*开始叫卖了":
                break;
            default:
                break;
        }
    }

    public static List<String> getTargets(String cmd_text, Nature[] natures) {
        List<Term> terms = HanLP.segment(cmd_text);
        List<String> targets = new ArrayList<>();
        for (int i = 0; i < terms.size(); i++) {
            Term term = terms.get(i);
            String nature = term.nature.toString();
            for (Nature n : natures) {
                if (nature.equals(n.toString())) {
                    targets.add(term.word);
                }
            }
        }
        return targets;
    }

    public static void searchMusic(MusicService musicService, String keyword) {
        int index = (int) musicService.searchMusic(keyword, false);
        if (index != -1) {
            TTSUtil.doSpeak("音乐加载中...");
            musicService.play(index);
        } else {
            TTSUtil.doSpeak((keyword.equals("笑") || keyword.equals("笑")) ? "哼，我不想" + keyword : "没有找到你想听的歌！");
        }
    }

    public static void startApp(Context ctx, AppInfo appInfo1) {
        if (appInfo1 != null) {
            AppUtil.startApp(ctx, appInfo1.getPackageName());
            TTSUtil.doSpeak("已为你^启动" + appInfo1.getAppName());
        } else TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
    }

    public static void handleWeather(FaceActivity ctx, String cmd_text, int type) {
        HFWeather.getInstance().setWeatherListener(ctx);
        if (cmd_text.startsWith("今")) {
            handleWeather(ctx, cmd_text, 0, type, 0);
        } else if (cmd_text.startsWith("明")) {
            handleWeather(ctx, cmd_text, 3, type, 1);
        } else if (cmd_text.startsWith("后")) {
            handleWeather(ctx, cmd_text, 7, type, 2);
        } else {
            TTSUtil.doSpeak("你说的什么呀？再说一次吧！");
        }
    }

    public static void handleWeather(Context ctx, String cmd_text, int days, int type, int index) {
        Logger.log(AIApp.TAG, "handleWeather", days, type, index);
        HFWeather.getInstance().setType(type);
        HFWeather.getInstance().setIndex(index);
        if (cmd_text.contains("今天")) {
            TTSUtil.doSpeak(HFWeather.getInstance().getWeatherNowStr());
        } else {
            HFWeather.getInstance().getWeather(ctx, days, BDConstants.getLatLog(), Lang.ZH_HANT, Unit.METRIC);
        }
    }

    public static void handlePlayMode(MusicService musicService, String cmd_text) {
        if (cmd_text.contains("单曲循环")) {
            musicService.setPlayMode(MusicService.MODE_REPEAT_SINGLE);
        } else if (cmd_text.contains("顺序播放")) {
            musicService.setPlayMode(MusicService.MODE_SEQUENTIAL);
        } else if (cmd_text.contains("循环播放")) {
            musicService.setPlayMode(MusicService.MODE_REPEAT_ALL);
        } else if (cmd_text.contains("随机播放")) {
            musicService.setPlayMode(MusicService.MODE_SHUFFLE);
        }
    }

    public static void handlePhone(Context ctx, String cmd_text) {
        if (cmd_text.contains("静音")) {
            AppUtil.setRingerMode(ctx, AudioManager.RINGER_MODE_SILENT);
        } else if (cmd_text.contains("闹铃")) {
            AppUtil.setRingerMode(ctx, AudioManager.RINGER_MODE_NORMAL);
        } else if (cmd_text.contains("振动")) {
            AppUtil.setRingerMode(ctx, AudioManager.RINGER_MODE_VIBRATE);
        }
    }

    /**
     * 上下点头实现
     */
    public static void upAndDownMove(BluetoothService btService, int currAngle, boolean up) { // 让舵机从初始位置向上转动20度
        int target = 0;
        if (up) {
            target = currAngle + 20;
        } else {
            target = currAngle - 20;
        }
        target = target < 0 ? 0 : target;
        target = target > 180 ? 180 : target;
        // 让舵机从上向下转动20度
        btService.write(BluetoothService.MSG_PWM1_SIGN, BluetoothUtil.angleToPwm(target, BluetoothService.MSG_PWM1_ANGLE_HALF));
    }

    public static void turnFrontAndBack(BluetoothService btService, int currAngle, boolean front) {
        int target = 0;
        if (front) {
            target = currAngle + 180;
        } else {
            target = currAngle - 180;
        }
        target = target < 0 ? 0 : target;
        target = target > 270 ? 270 : target;
        // 让舵机从上向下转动20度
        btService.write(BluetoothService.MSG_PWM3_SIGN, BluetoothUtil.angleToPwm(target, BluetoothService.MSG_PWM3_ANGLE_HALF));
    }

    /**
     * 上下点头实现
     */
    public static void leftAndRightMove(BluetoothService btService, int currAngle, boolean left) { // 让舵机从初始位置向上转动20度
        int target = 0;
        if (left) {
            target = currAngle - 30;
        } else {
            target = currAngle + 30;
        }
        target = target < 0 ? 0 : target;
        target = target > 270 ? 270 : target;
        // 让舵机从上向下转动20度
        btService.write(BluetoothService.MSG_PWM3_SIGN, BluetoothUtil.angleToPwm(target, BluetoothService.MSG_PWM3_ANGLE_HALF));
    }

    /**
     * 上下点头实现
     */
    public static void upAndDownMove(BluetoothService btService, int currAngle) { // 让舵机从初始位置向上转动20度
        int mid, up, down = 0;
        mid = currAngle;
        mid = currAngle <= 20 ? 20 : currAngle;
        mid = currAngle >= 160 ? 160 : currAngle;
        up = mid - 20;
        down = mid + 20;
        for (int i = 0; i < 4; i++) {
            // 让舵机从上向下转动20度
            btService.write(BluetoothService.MSG_PWM1_SIGN, BluetoothUtil.angleToPwm(down, BluetoothService.MSG_PWM1_ANGLE));
            btService.write(BluetoothService.MSG_PWM1_SIGN, BluetoothUtil.angleToPwm(up, BluetoothService.MSG_PWM1_ANGLE));
        }
        btService.write(BluetoothService.MSG_PWM1_SIGN, BluetoothUtil.angleToPwm(mid, BluetoothService.MSG_PWM1_ANGLE));
    }

    /**
     * 左右摇头实现
     */
    public static void leftAndRightMove(BluetoothService btService, int currAngle) { // 让舵机从初始位置向上转动20度
        int mid, left, right = 0;
        mid = currAngle;
        mid = currAngle <= 50 ? 50 : currAngle;
        mid = currAngle >= 220 ? 220 : currAngle;
        left = mid - 50;
        right = mid + 50;
        for (int i = 0; i < 4; i++) {
            btService.write(BluetoothService.MSG_PWM3_SIGN, BluetoothUtil.angleToPwm(left, BluetoothService.MSG_PWM3_ANGLE));
            // 让舵机从上向下转动20度
            btService.write(BluetoothService.MSG_PWM3_SIGN, BluetoothUtil.angleToPwm(right, BluetoothService.MSG_PWM3_ANGLE));
        }
        btService.write(BluetoothService.MSG_PWM3_SIGN, BluetoothUtil.angleToPwm(mid, BluetoothService.MSG_PWM3_ANGLE));
    }

}
